package engine.systems.render.perlinNoise
{
	import engine.classes.enumerations.SystemPriority;
	import engine.groups.Group;
	import engine.systems.render.perlinNoise.PerlinNoise;
	import engine.systems.render.perlinNoise.PerlinNoiseNode;
	import engine.systems.update.GameSystem;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	
	public class PerlinNoiseSystem extends GameSystem
	{
		public function PerlinNoiseSystem()
		{
			super(PerlinNoiseNode, updateNode);
			
			this.priority = SystemPriority.POST_RENDER;
		}
		
		private function updateNode(node:PerlinNoiseNode, time:Number):void
		{
			var noise:PerlinNoise = node.noise;
			var numOctaves:int = Math.min(noise.offsets.length, noise.rates.length);
			
			for(var i:int = 0; i < numOctaves; i++)
			{
				var offset:Point = noise.offsets[i];
				var rate:Point = noise.rates[i];
				
				offset.x += rate.x * time;
				offset.y += rate.y * time;
			}
			
			var data:BitmapData = Bitmap(node.render.display).bitmapData;
			data.perlinNoise(data.width, data.height, numOctaves, noise.seed, noise.stitch, noise.fractalNoise, noise.channels, noise.grayscale, noise.offsets);
		}
	}
}